const state = {
  courseList: [],
  courseDetail: null,
  favorites: JSON.parse(localStorage.getItem('favorites')) || []
}

const mutations = {
  SET_COURSE_LIST(state, list) {
    state.courseList = list
  },
  SET_COURSE_DETAIL(state, detail) {
    state.courseDetail = detail
  },
  ADD_COURSE(state, course) {
    state.courseList.push(course)
  },
  UPDATE_COURSE(state, course) {
    const index = state.courseList.findIndex(c => c.id === course.id)
    if (index !== -1) {
      state.courseList[index] = course
    }
  },
  DELETE_COURSE(state, courseId) {
    state.courseList = state.courseList.filter(c => c.id !== courseId)
  },
  ADD_FAVORITE(state, courseId) {
    if (!state.favorites.includes(courseId)) {
      state.favorites.push(courseId)
      localStorage.setItem('favorites', JSON.stringify(state.favorites))
    }
  },
  REMOVE_FAVORITE(state, courseId) {
    state.favorites = state.favorites.filter(id => id !== courseId)
    localStorage.setItem('favorites', JSON.stringify(state.favorites))
  }
}

const actions = {
  // 获取课程列表
  fetchCourseList({ commit }) {
    // 模拟API请求
    const mockList = [
      { id: 1, title: 'Vue3 入门', desc: '零基础掌握 Vue3', teacher: '张老师', category: '前端开发', img: 'https://img1.imgtp.com/2023/07/01/abc1.jpg', tags: ['前端开发'] },
      { id: 2, title: 'Python 数据分析', desc: '数据科学与AI入门', teacher: '李老师', category: '人工智能', img: 'https://img1.imgtp.com/2023/07/01/abc2.jpg', tags: ['人工智能'] },
      { id: 3, title: 'UI 设计基础', desc: '成为设计达人', teacher: '王老师', category: '设计', img: 'https://img1.imgtp.com/2023/07/01/abc3.jpg', tags: ['设计'] },
      { id: 4, title: 'React 实战', desc: '掌握 React 开发', teacher: '赵老师', category: '前端开发', img: 'https://img1.imgtp.com/2023/07/01/abc4.jpg', tags: ['前端开发'] },
      { id: 5, title: 'Node.js 入门', desc: '后端开发基础', teacher: '钱老师', category: '后端开发', img: 'https://img1.imgtp.com/2023/07/01/abc5.jpg', tags: ['后端开发'] }
    ]
    commit('SET_COURSE_LIST', mockList)
  },

  // 获取课程详情
  fetchCourseDetail({ commit }, courseId) {
    // 模拟API请求
    const mockDetail = {
      id: courseId,
      title: 'Vue3 入门',
      desc: '零基础掌握 Vue3',
      teacher: '张老师',
      category: '前端开发',
      img: 'https://img1.imgtp.com/2023/07/01/abc1.jpg',
      tags: ['前端开发'],
      chapters: [
        { id: 1, title: '第一章：Vue3 基础' },
        { id: 2, title: '第二章：组件化开发' }
      ]
    }
    commit('SET_COURSE_DETAIL', mockDetail)
  },

  // 添加课程
  addCourse({ commit }, course) {
    commit('ADD_COURSE', course)
  },

  // 更新课程
  updateCourse({ commit }, course) {
    commit('UPDATE_COURSE', course)
  },

  // 删除课程
  deleteCourse({ commit }, courseId) {
    commit('DELETE_COURSE', courseId)
  },

  // 添加收藏
  addFavorite({ commit }, courseId) {
    commit('ADD_FAVORITE', courseId)
  },

  // 取消收藏
  removeFavorite({ commit }, courseId) {
    commit('REMOVE_FAVORITE', courseId)
  }
}

const getters = {
  courseList: state => state.courseList,
  courseDetail: state => state.courseDetail,
  favorites: state => state.favorites,
  isFavorite: state => courseId => state.favorites.includes(courseId)
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
} 